


#include "boardIO.h"

/*=============================================================================
ADC INITIALIZATION FOR CHANNEL SCAN
=============================================================================*/
void initAdc1(void)
{

        AD1CON1bits.FORM   = 1;		// valeur en entier signee 	10 bit sur 16 bit
        AD1CON1bits.SSRC   = 2;		// Sample Clock Source: GP Timer starts conversion
        AD1CON1bits.ASAM   = 1;		// ADC Sample Control: Sampling begins immediately after conversion
        AD1CON1bits.AD12B  = 0;		// 10-bit ADC operation

        AD1CON2bits.CSCNA = 1;		// Scan Input Selections for CH0+ during Sample A bit
        AD1CON2bits.CHPS  = 0;		// Converts CH0


        AD1CON3bits.ADRC = 0;		// ADC Clock is derived from Systems Clock
        AD1CON3bits.ADCS = 63;		// ADC Conversion Clock Tad=Tcy*(ADCS+1)= (1/40M)*34 = 1.6us (625Khz)
                                                                // ADC Conversion Time for 10-bit Tc=12*Tab = 19.2us

        AD1CON2bits.SMPI    = (NUM_CHS2SCAN-1);	// nombre de ADC Channel qui seront scanne
        IFS0bits.AD1IF = 0;			// Clear the A/D interrupt flag bit
       
}




int moyenne_adc(int *chan)  //réalise une moyenne des ADC mesurée
{
    int cpt=0;
    int adc_conv=0;
    for(cpt=0; cpt<SAMP_BUFF_SIZE; cpt++)
    {
        adc_conv+=chan[cpt];
    }
   return adc_conv/=SAMP_BUFF_SIZE; 
}

int adc_to_mV(int adc)
{
    return adc*coef_adc;
}

